package com.exam.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.exam.exception.MajorIsExistException;
import com.exam.exception.StudentNonExist;
import com.exam.mapper.ClassesMapper;
import com.exam.mapper.MajorMapper;
import com.exam.pojo.Classes;
import com.exam.pojo.Major;
import com.exam.pojo.MajorAndClasses;
import com.exam.pojo.Paging;
import com.exam.pojo.Student;
import com.exam.service.MajorService;
import com.google.gson.Gson;


/**
 * @author 蔡小凤
 * 专业服务层实现类
 */
@Service("majorServiceImpl")
public class MajorServiceImpl implements MajorService {

	@Resource(name="majorMapper")
	private MajorMapper majorMapper;
	@Resource(name="classesMapper")
	private ClassesMapper classesMapper;
	@Resource(name="paging")
	private Paging<Major> paging;
	@Resource(name="gson")
	private Gson gson;
	
	/**
	 * 获取所有专业
	 * @param void
	 * @return List<Major> 专业列表 
	 */
	@Override
	public List<Major> getAllMajor() {
		return majorMapper.getAllMajor();
	}
	
	/**
	 * 通过专业名称获取专业对象
	 * @param majorName 专业名称
	 * @return 专业对象
	 */
	@Override
	public Major getMajorByName(String majorName) {
		return majorMapper.getMajorByName(majorName);
	}

	/**
	 * 获取所有专业和班级
	 * 
	 */
	@Override
	public List<MajorAndClasses> getAllMajorAndClasses() {
		List<Major> list = majorMapper.getAllMajor();
		List majorAndClassesList = new ArrayList<MajorAndClasses>(); //班级和专业的列表
		for(Major major:list) { //循环遍历专业列表
			MajorAndClasses m = new MajorAndClasses(); //专业与班级对象
			List<Classes> classesList = classesMapper.getClassesByMajorId(major.getId());//当前专业对应的班级列表
			List<String> classesNameList = new ArrayList<String>();
			for (Classes c:classesList) {
				
				classesNameList.add(c.getClassesName());
				m.setClassesName(classesNameList); //放入该专业下的所有班级的名称
			}
			if(0 == classesList.size()) { //如果专业中没有班级
				List<String> classesNameList1 = new ArrayList<String>();
				m.setClassesName(classesNameList1); 
			}
			m.setMajorName(major.getMajorName()); //放入专业名称
			majorAndClassesList.add(m); //将专业与班级对象放入列表
		}
		return majorAndClassesList;
	}

	/**
	 * 添加专业
	 * @param majorName 要添加的专业名称
	 * @return void
	 * @throws MajorIsExistException 专业已存在异常
	 */
	@Override
	public void addMajor(String majorName) throws MajorIsExistException {
		if(null != majorMapper.getMajorByName(majorName)) { //如果专业名称已经存在
			throw new MajorIsExistException();
		} else {
			majorMapper.addMajor(majorName);
		}
	}
	
	/**
	 * 更新专业
	 * @param majorName 原来的专业名称
	 * @param newName 新的专业名称
	 * @return void
	 * @throws MajorIsExistException 专业已存在异常
	 */
	@Override
	public void updateMajor(String majorName,String newName) throws MajorIsExistException {
		if(null != majorMapper.getMajorByName(newName) && !majorName.equals(newName)) { //如果要重新命名成的专业已存在并且不是原来的名称
			throw new MajorIsExistException();
		} else {
			Major major = majorMapper.getMajorByName(majorName);
			Map map = new HashMap<Object,Object>();
			map.put("newName",newName); //新名称
			map.put("majorId",major.getId()); 
			majorMapper.updateMajor(map); //更新
		}
	}

	/**
	 * 删除专业
	 * @param majorName 专业名称
	 */
	@Override
	public void deleteMajor(String majorName) {
		Major major = majorMapper.getMajorByName(majorName);
		majorMapper.deleteMajor(major.getId());
		//把专业下的班级也删除
		List<Classes> classesList =  classesMapper.getClassesByMajorId(major.getId());
		for (Classes c:classesList) { 
			classesMapper.deleteClasses(c.getId());
		}
	}

	/**
	 * 获取已经被删除的专业
	 * @param void  
	 * @return 专业列表
	 */
	@Override
	public Paging getDeletedMajorByPaging(Integer page,Integer pageSize) {
		Map map = new HashMap();
		map.put("start",(page-1)*pageSize);
		map.put("offset", pageSize);
		paging.setCurrentPage(page);
		paging.setPageSize(pageSize);
		paging.setTotalCount(majorMapper.getDeletedTotalCount()); //获取总记录数
		paging.setList(majorMapper.getDeletedMajorByPaging(map));
		return paging;
	}

	/**
	 * 批量恢复专业
	 * @param majorIdList 专业编号列表
	 * @return void 
	 */
	@Override
	public void recoverMajorByQuery(String majorIdList) {
		String[] list1 = gson.fromJson(majorIdList,String[].class); //获取要删除的学生编号
		for(String majorId:list1) { //循环遍历恢复
			Integer majorId1 = Integer.parseInt(majorId);
			majorMapper.recoverMajor(majorId1);
			//把该专业下的班级恢复
			List<Classes> classeList = classesMapper.getDeletedClassesByMajorId(majorId1); //获取该专业下被删除的班级列表
			for (Classes c:classeList) {
				classesMapper.recoverClasses(c.getId());
			}
		}
	}

	/**
	 * 恢复专业
	 * @param majorId 专业编号
	 * @return void
	 */
	@Override
	public void recoverMajor(Integer majorId) {
		majorMapper.recoverMajor(majorId);
		List<Classes> classeList = classesMapper.getDeletedClassesByMajorId(majorId); //获取该专业下被删除的班级列表
		for (Classes c:classeList) {
			classesMapper.recoverClasses(c.getId());
		}
	}
	
}
